package okhttp3.internal.connection;

import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.List;
import o.ftt;
import o.ftz;
import o.fuf;
import o.fuh;
import o.fum;
import o.fuo.d;
import o.fus;
import o.fuv;
import o.fux;
import o.fvg;
import o.fvm;
import o.fvm.b;
import o.fvn;
import o.fvv;
import o.fwf;
import okhttp3.Route;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.StreamResetException;

public final class StreamAllocation {
    static final /* synthetic */ boolean $assertionsDisabled = (!StreamAllocation.class.desiredAssertionStatus());
    public final ftt address;
    public final ftz call;
    private final Object callStackTrace;
    private boolean canceled;
    private fvv codec;
    private RealConnection connection;
    private final int connectionAttemptDelay;
    private final fuf connectionPool;
    public final fum eventListener;
    private int refusedStreamCount;
    private boolean released;
    private boolean reportedAcquired;
    private Route route;
    private b routeSelection;
    private final fvm routeSelector;

    public static final class e extends WeakReference<StreamAllocation> {
        public final Object ˋ;

        e(StreamAllocation streamAllocation, Object obj) {
            super(streamAllocation);
            this.ˋ = obj;
        }
    }

    public StreamAllocation(fuf o_fuf, ftt o_ftt, ftz o_ftz, fum o_fum, Object obj, int i) {
        this.connectionPool = o_fuf;
        this.address = o_ftt;
        this.routeSelector = new fvm(o_ftt, routeDatabase(), o_ftz, o_fum);
        this.call = o_ftz;
        this.eventListener = o_fum;
        this.callStackTrace = obj;
        this.connectionAttemptDelay = i;
    }

    public StreamAllocation(fuf o_fuf, ftt o_ftt, ftz o_ftz, fum o_fum, Object obj) {
        this.connectionPool = o_fuf;
        this.address = o_ftt;
        this.call = o_ftz;
        this.eventListener = o_fum;
        this.routeSelector = new fvm(o_ftt, routeDatabase(), o_ftz, o_fum);
        this.callStackTrace = obj;
        this.connectionAttemptDelay = 0;
    }

    public void newHttp2Connection(fuv o_fuv, fus o_fus) throws IOException {
        int ˋ = o_fuv.ˋ();
        int ˏ = o_fuv.ˏ();
        int ॱ = o_fuv.ॱ();
        int ˊ = o_fuv.ˊ();
        boolean ᐝॱ = o_fuv.ᐝॱ();
        if (!o_fus.ʼ()) {
            throw new IllegalArgumentException("a normal Request without http2ConnectionIndex");
        } else if (Integer.parseInt(o_fus.ˋ("Http2ConnectionIndex")) > this.connectionPool.ˏ(this.address)) {
            RealConnection realConnection;
            if (this.route == null && (this.routeSelection == null || !this.routeSelection.ˋ())) {
                this.routeSelection = this.routeSelector.ॱ();
            }
            synchronized (this.connectionPool) {
                Route ॱ2 = this.routeSelection.ॱ();
                this.route = ॱ2;
                realConnection = new RealConnection(this.connectionPool, ॱ2);
                if (this.routeSelection.ˎ()) {
                    realConnection.prepareConcurrentConnect(this.routeSelection.ˏ(), this.connectionAttemptDelay);
                    realConnection.setRouteSelection(this.routeSelection);
                }
            }
            realConnection.connect(ˋ, ˏ, ॱ, ˊ, ᐝॱ, this.call, this.eventListener);
            this.routeSelection.ॱ(realConnection.route());
            realConnection.keepaliveTimestampNs = System.nanoTime();
            synchronized (this.connectionPool) {
                fux.ˏ.ॱ(this.connectionPool, realConnection);
            }
        }
    }

    public fvv newStream(fuv o_fuv, d dVar, boolean z) {
        try {
            fvv newCodec = findHealthyConnection(o_fuv, dVar.ˏ(), dVar.ˋ(), dVar.ˎ(), o_fuv.ˊ(), o_fuv.ᐝॱ(), z).newCodec(o_fuv, dVar, this);
            synchronized (this.connectionPool) {
                this.codec = newCodec;
            }
            return newCodec;
        } catch (IOException e) {
            throw new RouteException(e);
        }
    }

    private RealConnection findHealthyConnection(fuv o_fuv, int i, int i2, int i3, int i4, boolean z, boolean z2) throws IOException {
        RealConnection findConnection;
        while (true) {
            findConnection = findConnection(o_fuv, i, i2, i3, i4, z);
            synchronized (this.connectionPool) {
                if (findConnection.successCount != 0) {
                    if (findConnection.isHealthy(z2)) {
                        break;
                    }
                    noNewStreams();
                } else {
                    break;
                }
            }
        }
        return findConnection;
    }

    private RealConnection findConnection(fuv o_fuv, int i, int i2, int i3, int i4, boolean z) throws IOException {
        fuh o_fuh;
        Socket releaseIfNoNewStreams;
        Object obj = null;
        fuh o_fuh2 = null;
        Route route = null;
        synchronized (this.connectionPool) {
            if (this.released) {
                throw new IllegalStateException("released");
            } else if (this.codec != null) {
                throw new IllegalStateException("codec != null");
            } else if (this.canceled) {
                throw new IOException("Canceled");
            } else {
                o_fuh = this.connection;
                releaseIfNoNewStreams = releaseIfNoNewStreams();
                if (this.connection != null) {
                    o_fuh2 = this.connection;
                    o_fuh = null;
                }
                if (!this.reportedAcquired) {
                    o_fuh = null;
                }
                if (o_fuh2 == null) {
                    fux.ˏ.ˏ(this.connectionPool, this.address, this, null);
                    if (this.connection != null) {
                        obj = 1;
                        o_fuh2 = this.connection;
                    } else {
                        route = this.route;
                    }
                }
            }
        }
        fvg.ˎ(releaseIfNoNewStreams);
        if (o_fuh != null) {
            this.eventListener.connectionReleased(this.call, o_fuh);
        }
        if (obj != null) {
            this.eventListener.connectionAcquired(this.call, o_fuh2);
        }
        if (o_fuh2 != null) {
            return o_fuh2;
        }
        Object obj2 = null;
        if (route == null && (this.routeSelection == null || !this.routeSelection.ˋ())) {
            obj2 = 1;
            this.routeSelection = this.routeSelector.ॱ();
        }
        synchronized (this.connectionPool) {
            if (this.canceled) {
                throw new IOException("Canceled");
            }
            if (obj2 != null) {
                List ˊ = this.routeSelection.ˊ();
                int size = ˊ.size();
                for (int i5 = 0; i5 < size; i5++) {
                    Route route2 = (Route) ˊ.get(i5);
                    fux.ˏ.ˏ(this.connectionPool, this.address, this, route2);
                    if (this.connection != null) {
                        obj = 1;
                        RealConnection realConnection = this.connection;
                        this.route = route2;
                        RealConnection realConnection2 = realConnection;
                        break;
                    }
                }
            }
            o_fuh = o_fuh2;
            if (obj == null) {
                Route ॱ;
                if (route == null) {
                    ॱ = this.routeSelection.ॱ();
                } else {
                    ॱ = route;
                }
                this.route = ॱ;
                this.refusedStreamCount = 0;
                realConnection2 = new RealConnection(this.connectionPool, ॱ);
                if (this.routeSelection.ˎ()) {
                    realConnection2.prepareConcurrentConnect(this.routeSelection.ˏ(), this.connectionAttemptDelay);
                    realConnection2.setRouteSelection(this.routeSelection);
                }
                acquire(realConnection2, false);
            }
        }
        if (obj != null) {
            this.eventListener.connectionAcquired(this.call, realConnection2);
            return realConnection2;
        }
        realConnection2.connect(i, i2, i3, i4, z, this.call, this.eventListener);
        this.routeSelection.ॱ(realConnection2.route());
        Socket socket = null;
        synchronized (this.connectionPool) {
            this.reportedAcquired = true;
            fux.ˏ.ॱ(this.connectionPool, realConnection2);
            if (realConnection2.isMultiplexed()) {
                Socket ˊ2 = fux.ˏ.ˊ(o_fuv, this.connectionPool, this.address, this);
                realConnection2 = this.connection;
                socket = ˊ2;
            }
        }
        fvg.ˎ(socket);
        this.eventListener.connectionAcquired(this.call, realConnection2);
        return realConnection2;
    }

    private Socket releaseIfNoNewStreams() {
        if ($assertionsDisabled || Thread.holdsLock(this.connectionPool)) {
            RealConnection realConnection = this.connection;
            if (realConnection == null || !realConnection.noNewStreams) {
                return null;
            }
            return deallocate(false, false, true);
        }
        throw new AssertionError();
    }

    public void streamFinished(boolean z, fvv o_fvv, long j, IOException iOException) {
        Socket deallocate;
        this.eventListener.responseBodyEnd(this.call, j);
        synchronized (this.connectionPool) {
            if (o_fvv != null) {
                if (o_fvv == this.codec) {
                    if (!z) {
                        RealConnection realConnection = this.connection;
                        realConnection.successCount++;
                        this.connection.consecutiveTimeoutCount = 0;
                    }
                    fuh o_fuh = this.connection;
                    deallocate = deallocate(z, false, true);
                    if (this.connection != null) {
                        o_fuh = null;
                    }
                    boolean z2 = this.released;
                }
            }
            throw new IllegalStateException("expected " + this.codec + " but was " + o_fvv);
        }
        fvg.ˎ(deallocate);
        if (o_fuh != null) {
            this.eventListener.connectionReleased(this.call, o_fuh);
        }
        if (iOException != null) {
            this.eventListener.callFailed(this.call, fux.ˏ.ˋ(this.call, iOException));
        } else if (z2) {
            fux.ˏ.ˋ(this.call, null);
            this.eventListener.callEnd(this.call);
        }
    }

    public fvv codec() {
        fvv o_fvv;
        synchronized (this.connectionPool) {
            o_fvv = this.codec;
        }
        return o_fvv;
    }

    private fvn routeDatabase() {
        return fux.ˏ.ˎ(this.connectionPool);
    }

    public Route route() {
        return this.route;
    }

    public synchronized RealConnection connection() {
        return this.connection;
    }

    public void release() {
        Socket deallocate;
        synchronized (this.connectionPool) {
            fuh o_fuh = this.connection;
            deallocate = deallocate(false, true, false);
            if (this.connection != null) {
                o_fuh = null;
            }
        }
        fvg.ˎ(deallocate);
        if (o_fuh != null) {
            fux.ˏ.ˋ(this.call, null);
            this.eventListener.connectionReleased(this.call, o_fuh);
            this.eventListener.callEnd(this.call);
        }
    }

    public void noNewStreams() {
        Socket deallocate;
        synchronized (this.connectionPool) {
            fuh o_fuh = this.connection;
            deallocate = deallocate(true, false, false);
            if (this.connection != null) {
                o_fuh = null;
            }
        }
        fvg.ˎ(deallocate);
        if (o_fuh != null) {
            this.eventListener.connectionReleased(this.call, o_fuh);
        }
    }

    private Socket deallocate(boolean z, boolean z2, boolean z3) {
        if ($assertionsDisabled || Thread.holdsLock(this.connectionPool)) {
            if (z3) {
                this.codec = null;
            }
            if (z2) {
                this.released = true;
            }
            if (this.connection == null) {
                return null;
            }
            if (z) {
                this.connection.noNewStreams = true;
            }
            if (this.codec != null) {
                return null;
            }
            if (!this.released && !this.connection.noNewStreams) {
                return null;
            }
            Socket socket;
            release(this.connection);
            if (this.connection.allocations.isEmpty()) {
                this.connection.idleAtNanos = System.nanoTime();
                if (fux.ˏ.ˊ(this.connectionPool, this.connection)) {
                    socket = this.connection.socket();
                    this.connection = null;
                    return socket;
                }
            }
            socket = null;
            this.connection = null;
            return socket;
        }
        throw new AssertionError();
    }

    public void cancel() {
        synchronized (this.connectionPool) {
            this.canceled = true;
            fvv o_fvv = this.codec;
            RealConnection realConnection = this.connection;
        }
        if (o_fvv != null) {
            o_fvv.ˋ();
        } else if (realConnection != null) {
            realConnection.cancel();
        }
    }

    public void streamFailed(IOException iOException) {
        Socket deallocate;
        boolean z = false;
        boolean z2 = true;
        synchronized (this.connectionPool) {
            if (iOException instanceof StreamResetException) {
                fwf o_fwf = ((StreamResetException) iOException).ˎ;
                if (o_fwf == fwf.REFUSED_STREAM) {
                    this.refusedStreamCount++;
                    if (this.refusedStreamCount > 1) {
                        this.route = null;
                    }
                    z2 = false;
                } else {
                    if (o_fwf != fwf.CANCEL) {
                        this.route = null;
                    }
                    z2 = false;
                }
                z = z2;
            } else if (this.connection == null || (this.connection.isMultiplexed() && !(iOException instanceof ConnectionShutdownException))) {
                if (this.connection != null && this.connection.isMultiplexed() && (iOException instanceof SocketTimeoutException)) {
                    RealConnection realConnection = this.connection;
                    realConnection.consecutiveTimeoutCount++;
                    if (this.connection.consecutiveTimeoutCount > this.connection.consecutiveTimeoutThreshold) {
                        z = true;
                    }
                }
            } else if (this.connection.successCount == 0) {
                if (!(this.route == null || iOException == null)) {
                    this.routeSelector.ॱ(this.route, iOException);
                    if (this.routeSelection != null) {
                        this.routeSelection.ʼ();
                    }
                }
                this.route = null;
                z = true;
            } else {
                z = true;
            }
            fuh o_fuh = this.connection;
            deallocate = deallocate(z, false, true);
            if (!(this.connection == null && this.reportedAcquired)) {
                o_fuh = null;
            }
        }
        fvg.ˎ(deallocate);
        if (o_fuh != null) {
            this.eventListener.connectionReleased(this.call, o_fuh);
        }
    }

    public void acquire(RealConnection realConnection, boolean z) {
        if (!$assertionsDisabled && !Thread.holdsLock(this.connectionPool)) {
            throw new AssertionError();
        } else if (this.connection == null || this.connection.noNewStreams) {
            this.connection = realConnection;
            this.reportedAcquired = z;
            realConnection.allocations.add(new e(this, this.callStackTrace));
        } else {
            throw new IllegalStateException();
        }
    }

    private void release(RealConnection realConnection) {
        int size = realConnection.allocations.size();
        for (int i = 0; i < size; i++) {
            if (((Reference) realConnection.allocations.get(i)).get() == this) {
                realConnection.allocations.remove(i);
                return;
            }
        }
        throw new IllegalStateException();
    }

    public Socket releaseAndAcquire(RealConnection realConnection) {
        if (!$assertionsDisabled && !Thread.holdsLock(this.connectionPool)) {
            throw new AssertionError();
        } else if (this.codec == null && this.connection.allocations.size() == 1) {
            Reference reference = (Reference) this.connection.allocations.get(0);
            Socket deallocate = deallocate(true, false, false);
            this.connection = realConnection;
            realConnection.allocations.add(reference);
            return deallocate;
        } else {
            throw new IllegalStateException();
        }
    }

    public boolean hasMoreRoutes() {
        return this.route != null || ((this.routeSelection != null && this.routeSelection.ˋ()) || this.routeSelector.ˋ());
    }

    public String toString() {
        RealConnection connection = connection();
        return connection != null ? connection.toString() : this.address.toString();
    }
}
